<template>
  <div style="padding-top: 10px">
    <div style="margin-left: 10px">
      <h4>接诊模板</h4>
    </div>
    <el-tree
      :data="tableData"
      :default-checked-keys="checkedId"
      node-key="id"
      :props="defaultProps"
      show-checkbox
      @check="handledeptNodeClick"
    />
    <div style="margin-left: 40%; margin-top: 30px">
      <el-button @click="goBack()">返回</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </div>
  </div>
</template>

<script>
  import {
    treatmentSetting,
    userTreatmentMenu,
    userTreatmentInfo,
  } from '@/api/reception'
  export default {
    components: {},
    props: {
      ids: {
        type: Number,
        required: true,
      },
    },
    data() {
      return {
        tableData: [],
        defaultProps: {
          label: 'menu_name',
        },
        list: [],
        checkedId: [],
      }
    },
    watch: {},

    mounted() {
      this.init()
    },
    methods: {
      // handleCheckChange(e) {
      //   // this.list.push(e.id)
      // },
      handledeptNodeClick(nodeObj, SelectedObj) {
        this.list = SelectedObj.checkedKeys
      },
      init() {
        const data = {
          user_id: this.ids,
        }
        userTreatmentInfo(data).then((e) => {
          console.log()
          e.data.forEach((k) => {
            this.checkedId.push(k.menu_id)
          })
        })
        treatmentSetting().then((e) => {
          this.tableData = e.data.data
        })
      },
      submit() {
        console.log(this.list)
        var newArr = [...new Set(this.list)] //利用了Set结构不能接收重复数据的特点
        const dada = {
          user_id: this.ids,
          ids: JSON.stringify(newArr),
        }
        console.log(dada)

        userTreatmentMenu(dada).then((e) => {
          console.log(e)
          this.goBack()
        })
      },
      // 返回
      goBack() {
        this.$emit('showMain', 0)
      },
    },
  }
</script>

<style></style>
